<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUIManager
 *
 * @author jianfeng
 */
abstract class BUIScreen extends BUIBase {

    protected $screenParent = null;
    protected $uiNode = null;
    protected $perviousScreen = null;
    protected $nextScreen = null;

    public function __construct($parent = NULL) {
        
        parent::__construct();
        
        if ($parent != null)
            $this->screenParent = $parent;
    }

    /**
     *
     * @return BUINode 
     */
    public function getUINode() {
        if ($this->screenParent instanceof BUIScreenFlow)
            return $this->screenParent->getUINode();

        return $this->screenParent;
    }

    /**
     *
     * @param type $uiNode 
     */
    public function setUINode($uiNode) {
        if ($this->screenParent instanceof BUIScreenFlow)
            $this->screenParent->setUINode($uiNode);
        else {
            $this->screenParent = $uiNode;
        }
    }

    public function getParent() {
        return $this->screenParent;
    }

    public function setParent($parent) {
        $this->screenParent = $parent;
    }

    public function renderView($view = null, $data = NULL) {

        if (!is_array($data))
            $data = array();

        $data['screen'] = $this;

        $controller = Yii::app()->getController();
        $controller->renderPartial('screen', array('view' => $view, 'data' => $data));
    }

    public abstract function render($data = null);

    public function getName() {
        $className = get_class($this);
        $screenName = substr($className, 7);
        return $screenName;
    }

    public function registerMenu($event) {
        ;
    }

    public function isMemberOfScreenFlow() {
        if ($this->screenParent instanceof BUIScreenFlow)
            return TRUE;

        return FALSE;
    }

    public function gotoScreen($name, $data = null) {

        $flow = NULL;
        $gotoFlowResult = TRUE;

        //first find flow, if can not goto in flow, will goto screen directly
        if ($this->screenParent != NULL && $this->screenParent instanceof BUIScreenFlow) {
            $flow = $this->screenParent;
            $gotoFlowResult = $flow->gotoScreen($name, $data);
        } else {
            parent::gotoScreen($name, $data);
        }

        if ($gotoFlowResult != NULL) {
            if (!$gotoFlowResult)
                parent::gotoScreen($name, $data);
        }
    }

}

?>
